package com.bbgwap.controller;

import com.alibaba.fastjson.JSON;
import com.bbgkh.model.BaseInfo;
import com.bbgkh.model.PO.CustomerPO;
import com.bbgkh.model.PO.SaleInfoPO;
import com.bbgkh.service.ISaleService;
import com.bbgkh.service.IUserService;
import com.bbgkh.utils.StringUtils;
import com.bbgkh.utils.UserUtils;
import com.bbgwap.model.WeChatInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.bbgwap.base.APPCONST.*;

/**
 * Created by lixiang on 04/04/2017.
 */
@Controller
public class ViewController extends BaseController{

    @Autowired
    private IUserService userService;

    @Autowired
    private ISaleService saleService;

    @RequestMapping("/")
    public String showHome(Map<String , Object> map){

        map.put("test","aaaaaa");
        logger.info("come in home");

        return "index";
    }

    @GetMapping(value = "member/memberPage")
    public String memberPage(){
        return "member/addMember";
    }

    //通用的页面跳转
    @RequestMapping("/view/{pageName}")
    public String showPage(@PathVariable String pageName, Map<String , Object> map){


        map.put("test","aaaaaa");
        logger.info("come in home");

        return "index";
    }

    //通过扫描二维码进行卖货
    @GetMapping(value = "sale/scanSale")
    public ModelAndView scanSale(@RequestParam(required = false, defaultValue = "0") String price,
                           @RequestParam(required = false,defaultValue = "0") String sysNo,
                           @RequestParam(required = false,defaultValue = "0") String size,
                           @RequestParam(required = false,defaultValue = "0") String color,
                           @RequestParam(required = false, defaultValue = "0") String backType,
                           HttpServletRequest request, HttpServletResponse response){

        BaseInfo baseInfo = null;
        Map<String , Object > map = new HashMap();
        if (backType.equals("1")) {
            WeChatInfo weChatInfo  = (WeChatInfo) request.getSession().getAttribute(WECHAT_INFO);
            logger.info("取出微信用户信息");
            List<CustomerPO> customerPOS = userService.validateUser(weChatInfo.getOpenid(),weChatInfo.getOpenid());
            if(customerPOS.size()>0){
                CustomerPO customerPO = new CustomerPO();
                customerPO.setUid(StringUtils.getUID(weChatInfo.getOpenid()));
                request.getSession().setAttribute("customer",customerPO);
                map.put("openId",weChatInfo.getOpenid());
                map.put("price",price);
                map.put("sysNo",sysNo);
                map.put("size",size);
                map.put("color",color);
                return new ModelAndView("scanSale",map);

            }else{
                try {
                    response.sendRedirect("http://www.bbgkh.shop");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return new ModelAndView();
        }else{
            try {
                String backUrl = WEBROOT +"/sale/scanSale/?price="+price
                        +"&sysNo="+sysNo
                        +"&size="+size
                        +"&backType=1";
                request.getSession().setAttribute(BACK_URL,backUrl);
                logger.info("放backurl");
                response.sendRedirect(WECHAT_OPENID);
                return null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return new ModelAndView();

    }

    //通过获取微信的id来实现自动登录
    @GetMapping(value = "login/wxAutoLogin")
    public String wxAutoLogin(
            @RequestParam(required = false, defaultValue = "0") String backType,
            HttpServletRequest request, HttpServletResponse response){

        BaseInfo baseInfo = null;
        Map<String , Object > map = new HashMap();
        if (backType.equals("1")) {
            WeChatInfo weChatInfo  = (WeChatInfo) request.getSession().getAttribute(WECHAT_INFO);
            logger.info("取出微信用户信息");
            List<CustomerPO> customerPOS = userService.validateUser(weChatInfo.getOpenid(),weChatInfo.getOpenid());
            //如果是我们的管理用户，则直接进入管理页面
            if(customerPOS.size()>0){
                map.put("openId",weChatInfo.getOpenid());
                CustomerPO customerPO = new CustomerPO();
                customerPO.setUid(StringUtils.getUID(weChatInfo.getOpenid()));
                request.getSession().setAttribute("customer",customerPO);
                return "home";

            }else{
                try {
                    response.sendRedirect("http://www.bbgkh.shop");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return JSON.toJSONString(baseInfo);
        }else{
            try {
                String backUrl = WEBROOT +"/login/wxAutoLogin"+"?backType=1";
                request.getSession().setAttribute(BACK_URL,backUrl);
                logger.info("放backurl");
                response.sendRedirect(WECHAT_OPENID);
                return "sale";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return JSON.toJSONString(baseInfo);

    }
}
